package com.linkme2night.model;

 

import android.annotation.SuppressLint;

import java.sql.Timestamp;
import org.w3c.dom.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public final class CommentDAO {

        private List<Comment> listCommments = new ArrayList<Comment>();
        private String[] cid;
        private String[] text;
        private Timestamp[] time;
        private Document doc;
        private NodeList nameList; 
        private NodeList cidList;
        private NodeList nodeList;
        private DateFormat sdf;
       
        //Constructor, currently loads the xml doc
        @SuppressLint("SimpleDateFormat")
		public CommentDAO()
        {
    		XmlCon XmlCon = new XmlCon();
            XmlCon.LoadFile();
            doc = XmlCon.getDoc();
            nodeList = doc.getElementsByTagName("Comment");
            cid = new String[nodeList.getLength()];
            time = new Timestamp[nodeList.getLength()];
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        
        //Add a comment to the xml file. Currently forces the timestamp to be null.
        public void add(Comment comment) {
        		
                String[] c = {"ClubberId", "VenueId", "Text", "Date"};
                String[] e = {Integer.toString(comment.getClubberID()), Integer.toString(comment.getVenueID()), comment.getText(), "null"};
            
                try{
                	CreateXml.CreateXml("Comment", c, e);
                }catch(Exception o){System.out.println(o.getMessage());}
                	listCommments.add(comment);
        	}
       
        //return comments based on venueId
        public List<Comment> getComments(int venueId) {
        	
                listCommments.clear();
                cid = getClubberId(venueId);
                text = getText(venueId);
                time = getTime(venueId);
                
                for(int i = 0; i < cid.length; i++){
                    try{
                    	listCommments.add(new Comment(text[i],time[i],Integer.parseInt(cid[i]),venueId));
                    }
                    catch(Exception e){System.out.println(e.getMessage());}
                }
                return listCommments;
        }

        //Returns comment based on venueId and userId
        public List<Comment> getLatestComment(int venueId, int clubberId) {

        	listCommments.clear();
            cid = getClubberId(venueId);
            text = getText(venueId);
            time = getTime(venueId);
            
            for(int i = 0; i < cid.length; i++){
                try{
                    if(Integer.parseInt(cid[i]) == (clubberId))
                    	listCommments.add(new Comment(text[i],time[i],Integer.parseInt(cid[i]),venueId));
                }
                catch(Exception e){System.out.println(e.getMessage());}
            }
            return listCommments;
        }
        
        //class utility methods 
        private String[] getClubberId(int id){
        	String[] output = new String[nodeList.getLength()];
            for (int i = 0; i < nodeList.getLength(); i++) {
 
                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;
                cidList = fstElmnt.getElementsByTagName("VenueId");
                Element cidElement = (Element) cidList.item(0);
                cidList = cidElement.getChildNodes();
  
                System.out.println("" + cidList.item(0).getNodeValue());
                if(cidList.item(0).getNodeValue().equals(""+id))
                {
                nameList = fstElmnt.getElementsByTagName("ClubberId");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                System.out.println((""
                        + ((Node) nameList.item(0)).getNodeValue()));
                output[i] = (""
                        + ((Node) nameList.item(0)).getNodeValue());
            
        }

        }
         return output;
    }
        
        private Timestamp[] getTime(int id){
            
            for (int i = 0; i < nodeList.getLength(); i++) {
 
                Node node = nodeList.item(i);


                Element fstElmnt = (Element) node;
                cidList = fstElmnt.getElementsByTagName("VenueId");
                Element cidElement = (Element) cidList.item(0);
                cidList = cidElement.getChildNodes();
                
                System.out.println("" + cidList.item(0).getNodeValue());
                if(cidList.item(0).getNodeValue().equals(""+id))
                {
                nameList = fstElmnt.getElementsByTagName("Date");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
                try{
                System.out.println("" + 
                        new Timestamp(sdf.parse("" + ((Node) nameList.item(0)).getNodeValue()).getTime()));
                time[i] = new Timestamp(sdf.parse(""
                        + ((Node) nameList.item(0)).getNodeValue()).getTime());
                    }
                catch(Exception e){System.out.println(e.getMessage());}
            }
            
        }
                
                return time;
        }
        
        private String[] getText(int id){
        	String[] output = new String[nodeList.getLength()];
            for (int i = 0; i < nodeList.getLength(); i++) {
            	
                Node node = nodeList.item(i);

                Element fstElmnt = (Element) node;
                cidList = fstElmnt.getElementsByTagName("VenueId");
                System.out.println(cidList.getLength());
                Element cidElement = (Element) cidList.item(0);
                 cidList = cidElement.getChildNodes();
                
                System.out.println("" + cidList.item(0).getNodeValue());
                if(cidList.item(0).getNodeValue().equals(""+id))
                {
                nameList = fstElmnt.getElementsByTagName("Text");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();
            output[i] = (""
                        + ((Node) nameList.item(0)).getNodeValue());
            
        }

        }
         return output;
        }
}